package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.mapper.UserTableMapper;
import com.qf.pojo.R;
import com.qf.pojo.UserDto;
import com.qf.pojo.UserTable;
import com.qf.service.UserService;
import com.qf.util.RUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserTableMapper userTableMapper;

    @Override
    public List<UserTable> findAll() {
        return userTableMapper.selectByExample(null);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        userTableMapper.deleteAll(ids);
    }

    @Override
    public List<UserTable> excel(Integer[] ids) {
        List<UserTable> users = userTableMapper.excel(ids);

        //生成Excel表格
        String file = "D:/excel/" + "贵美" + System.currentTimeMillis() + ".xlsx";
        //获取当前文件对象
        File currFile = new File(file);
        //获取父目录文件对象
        File parentFile = currFile.getParentFile();
        //判断文件对象的父目录是否存在
        if (!parentFile.exists()) {
            //连同子目录一起创建
            parentFile.mkdirs();
        }
        //写入
        EasyExcel.write(file, UserTable.class).sheet("模板").doWrite(users);

        return users;
    }

    @Override
    public void updateUser(UserTable userTable) {
        userTableMapper.updateByPrimaryKeySelective(userTable);
    }

    @Override
    public void deleteUserById(Integer id) {
        userTableMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addUser(UserTable userTable) {
        userTableMapper.insertSelective(userTable);
    }

}
